home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
gnat1792.zip
/
gnat179b
/
t-adainc
/
a-ngcoty.adb
< prev
next >
Wrap
Text File
|
1994-05-19
|
14KB
|
534 lines
------------------------------------------------------------------------------
-- --
-- GNAT RUNTIME COMPONENTS --
-- --
-- A D A . N U M E R I C S . G E N E R I C _ C O M P L E X _ T Y P E S --
-- --
-- B o d y --
-- --
-- $Revision: 1.3 $ --
-- --
-- Copyright (c) 1992,1993,1994 NYU, All Rights Reserved --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
-- --
------------------------------------------------------------------------------
with Ada.Numerics.Aux; use Ada.Numerics.Aux;
package body Ada.Numerics.Generic_Complex_Types is
subtype R is Real'Base;
---------
-- "+" --
---------
function "+" (Right : Complex) return Complex is
begin
return Right;
end "+";
function "+" (Left, Right : Complex) return Complex is
begin
return Complex'(Left.Re + Right.Re, Left.Im + Right.Im);
end "+";
function "+" (Right : Imaginary) return Imaginary is
begin
return Right;
end "+";
function "+" (Left, Right : Imaginary) return Imaginary is
begin
return Imaginary (R (Left) + R (Right));
end "+";
function "+" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re + Right, Left.Im);
end "+";
function "+" (Left : Real'Base; Right : Complex) return Complex is
begin
return Complex'(Left + Right.Re, Right.Im);
end "+";
function "+" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(Left.Re, Left.Im + R (Right));
end "+";
function "+" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(R (Left) + Right.Re, Right.Im);
end "+";
function "+" (Left : Imaginary; Right : Real'Base) return Complex is
begin
return Complex'(Right, R (Left));
end "+";
function "+" (Left : Real'Base; Right : Imaginary) return Complex is
begin
return Complex'(Left, R (Right));
end "+";
---------
-- "-" --
---------
function "-" (Right : Complex) return Complex is
begin
return (-Right.Re, -Right.Im);
end "-";
function "-" (Left, Right : Complex) return Complex is
begin
return (Left.Re - Right.Re, Left.Im - Right.Im);
end "-";
function "-" (Right : Imaginary) return Imaginary is
begin
return Imaginary (-R (Right));
end "-";
function "-" (Left, Right : Imaginary) return Imaginary is
begin
return Imaginary (R (Left) - R (Right));
end "-";
function "-" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re - Right, Left.Im);
end "-";
function "-" (Left : Real'Base; Right : Complex) return Complex is
begin
return Complex'(Left - Right.Re, -Right.Im);
end "-";
function "-" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(Left.Re, Left.Im - R (Right));
end "-";
function "-" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(R (Left) - Right.Re, -Right.Im);
end "-";
function "-" (Left : Imaginary; Right : Real'Base) return Complex is
begin
return Complex'(-Right, R (Left));
end "-";
function "-" (Left : Real'Base; Right : Imaginary) return Complex is
begin
return Complex'(Left, -R (Right));
end "-";
---------
-- "*" --
---------
function "*" (Left, Right : Complex) return Complex is
begin
return (Re => Left.Re * Right.Re - Left.Im * Right.Im,
Im => Left.Re * Right.Im + Left.Im * Right.Re);
end "*";
function "*" (Left, Right : Imaginary) return Real'Base is
begin
return -R (Left) * R (Right);
end "*";
function "*" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re * Right, Left.Im * Right);
end "*";
function "*" (Left : Real'Base; Right : Complex) return Complex is
begin
return (Left * Right.Re, Left * Right.Im);
end "*";
function "*" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(-(Left.Im * R (Right)), Left.Re * R (Right));
end "*";
function "*" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(-(R (Left) * Right.Im), R (Left) * Right.Re);
end "*";
function "*" (Left : Imaginary; Right : Real'Base) return Imaginary is
begin
return Left * Imaginary (Right);
end "*";
function "*" (Left : Real'Base; Right : Imaginary) return Imaginary is
begin
return Imaginary (Left * R (Right));
end "*";
---------
-- "/" --
---------
function "/" (Left, Right : Complex) return Complex is
a : constant R := Left.Re;
b : constant R := Left.Im;
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
return Complex'(Re => ((a * c) + (b * d)) / (c ** 2 + d ** 2),
Im => ((b * c) - (a * d)) / (c ** 2 + d ** 2));
end "/";
function "/" (Left, Right : Imaginary) return Real'Base is
begin
return R (Left) / R (Right);
end "/";
function "/" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re / Right, Left.Im / Right);
end "/";
function "/" (Left : Real'Base; Right : Complex) return Complex is
a : constant R := Left;
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
return Complex'(Re => (a * c) / (c ** 2 + d ** 2),
Im => -(a * d) / (c ** 2 + d ** 2));
end "/";
function "/" (Left : Complex; Right : Imaginary) return Complex is
a : constant R := Left.Re;
b : constant R := Left.Im;
d : constant R := R (Right);
begin
return (b / d, -a / d);
end "/";
function "/" (Left : Imaginary; Right : Complex) return Complex is
b : constant R := R (Left);
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
return (Re => -b * d / (c ** 2 + d ** 2),
Im => b * c / (c ** 2 + d ** 2));
end "/";
function "/" (Left : Imaginary; Right : Real'Base) return Imaginary is
begin
return Imaginary (R (Left) / Right);
end "/";
function "/" (Left : Real'Base; Right : Imaginary) return Imaginary is
begin
return Imaginary (-Left / R (Right));
end "/";
----------
-- "**" --
----------
function "**" (Left : Complex; Right : Integer) return Complex is
Result : Complex := (1.0, 0.0);
Factor : Complex := Left;
Exp : Natural := Right;
begin
-- We use the standard logarithmic approach, Exp gets shifted right
-- testing successive low order bits and Factor is the value of the
-- base raised to the next power of 2. For positive exponents we
-- multiply the result by this factor, for negative expo